#include "logininterface.h"
#include "QHBoxLayout"
#include "QVBoxLayout"
#include "QDebug"
#include "QMessageBox"


logininterface::logininterface(QString label, QWidget * parent)
    : QDialog(parent)
{
    this->loginUser = "Unset";
    this->setWindowTitle(label);

    this->userNameLabel = new QLabel("用户名", this);
    this->userNameEdit = new QLineEdit(this);
    this->passWordLabel = new QLabel("密  码", this);
    this->passWordEdit = new QLineEdit(this);
    this->loginButton = new QPushButton("登录", this);

    this->userNameLabel->setBuddy(this->userNameEdit);
    this->passWordLabel->setBuddy(this->passWordEdit);

    connect(loginButton, SIGNAL(clicked()), this, SLOT(onLoginButtonClicked()));
    initLayout();
}

logininterface::~logininterface() {
    qDebug() << "登录页面析构";
}

void logininterface::initLayout() {
    QHBoxLayout * usnmLayout = new QHBoxLayout,
                * pswdLayout = new QHBoxLayout;
    QVBoxLayout * mainLayout = new QVBoxLayout;
    // QVBoxLayout * boxEndl = new QVBoxLayout;

    usnmLayout->addWidget(this->userNameLabel);
    usnmLayout->addWidget(this->userNameEdit);

    pswdLayout->addWidget(this->passWordLabel);
    // pswdLayout->addStretch();
    pswdLayout->addWidget(this->passWordEdit);

    mainLayout->addLayout(usnmLayout);
    mainLayout->addLayout(pswdLayout);
    // mainLayout->addLayout(boxEndl);
    mainLayout->addWidget(this->loginButton);

    this->setLayout(mainLayout);
}

void logininterface::onLoginButtonClicked() {
    qDebug() << getUserName() << Qt::endl << getPassWord();
    QString userName = getUserName(), passWord = getPassWord();
    if (userName != passWord) {
        QMessageBox msgBox;
        msgBox.setWindowTitle("错误");
        msgBox.setText("<center>用户名或密码错误</center>");
        msgBox.setTextFormat(Qt::RichText);
        msgBox.exec();
    } else {
        this->accept();
    }
}

QString logininterface::getUserName() {
    QString username = this->userNameEdit->text();
    if(username.isEmpty())
        username = "user1";
    this->loginUser = username;
    return username;
}

QString logininterface::getPassWord() {
    QString password = this->passWordEdit->text();
    if(password.isEmpty())
        password = "user1";
    return password;
}

QString logininterface::getReturnStr() {
    return this->loginUser;
}
